<?php

/*
Zoo Management Simulation
Displays the cage the user has selected from the Admin UI.
The file also toggles lighting for the cage and secures all doors for the cage
The user can add cage entries which is handled by the addNumberofEntries file and edit the cage through the cageProcess file
*/

include("dbconnect.php");

if ($_REQUEST['cageid']){
	$cagesql = "SELECT * FROM enclosure WHERE enclosureId = '$_GET[cageid]'";
	$entrysql = "SELECT * FROM entry WHERE enclosureId = '$_GET[cageid]'";
	$cagenumber = $_GET['cagenumber'];
} else
{echo "Error";
}

	foreach ($dbh->query($cagesql) as $cagerow)
	{
		$id = $cagerow[0];
		$name = $cagerow[1];
		$size = $cagerow[2];
		$exhibitname = $cagerow[4];
		$exhibitdesc = $cagerow [5];
		$numberofentries = $cagerow[6];
		$light = $cagerow[7];
		$locked = $cagerow[8];
		$animaldetector = $cagerow[9];
		$humandetector = $cagerow[10];
		$typesql = "SELECT * FROM enclosuretype WHERE enclosureTypeId = ".$cagerow[3];
		foreach ($dbh->query($typesql) as $typerow)
		{
			$type = $typerow[1];
		}
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Zoo Management Cage Page</title>
<link href="mainstyle.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1 align="center">Cage</h1>

<fieldset>
<div id="fieldtitle" align="center">
<?php
if($locked == 0){echo "Cage is unlocked";}
elseif($locked == 1){echo "Cage is locked";}
?>
</div>
</fieldset>

<table width="100%">
<tr width="100%">
<td width="50%">
<fieldset>
<legend id="fieldtitle" align="center">Cage Entries</legend>
<?php
			$entrynumber = 1;
			foreach ($dbh->query($entrysql) as $entrytablerow)
			{
				echo "<p><form id='entryForm' name='entryForm' method='post' enctype='multipart/form-data' action='cageProcess.php?entryid=".$entrytablerow[0]."&entrystatus=".$entrytablerow[3]."'>";
				echo "<label id='entrytitle'>Entry</label><label>". $entrynumber . "</label><br />";
				$entrytypesql = "SELECT * FROM entrytype WHERE entryTypeId =".$entrytablerow[2];
				foreach ($dbh->query($entrytypesql) as $entrytyperow)
				{
				echo "<label id='entrytitle'>Entry Type</label><label>". $entrytyperow[1] . "</label><br />";
				}
				
				echo "<label id='entrytitle'>Entry Status</label>";
				if($entrytablerow[3] == 0){echo "<label>Closed</label>";}
				elseif($entrytablerow[3] == 1){echo "<label>Open</label>";}
				echo "<br />";
				
				
				echo "<input class='entrysubmit' type='submit' name='submit' value='Toggle door' />";
				
				
				echo "<input type='hidden' name='cageid' value='".$id . "' />";
				echo "<input type='hidden' name='cagenumber' value='".$cagenumber . "' />";
				echo "<input type='hidden' name='lighting' value='".$light . "' />";
				
				echo"</form></p>";
				$entrynumber+=1;
			}
?>
<form id="cageForm" name="cageForm" method="post" enctype="multipart/form-data" action="cageProcess.php">
<?php
echo "<input type='hidden' name='cageid' value='".$id . "' />";
echo "<input type='hidden' name='cagenumber' value='".$cagenumber . "' />";

echo "<input class='entrysubmit' type='submit' name='submit' value='Secure doors' />";?>
</form>
</fieldset>
</td>

<td width="50%">
<fieldset>
<legend id="fieldtitle" align="center">Occupied Status</legend>

<form id='detectorForm' name='detectorForm' method='post' enctype='multipart/form-data' action='cageProcess.php'>
<p><label id="titleform">Human Detector</label><label>
<?php
echo "<input type='hidden' name='cageid' value='".$id . "' />";
echo "<input type='hidden' name='cagenumber' value='".$cagenumber . "' />";
echo "<input type='hidden' name='humandetector' value='".$humandetector . "' />";
echo "<input type='hidden' name='animaldetector' value='".$animaldetector . "' />";

if($humandetector == 0){echo "No human inside";}
elseif($humandetector == 1){echo "Human Inside";}
?></label><br />
<input class='entrysubmit' type='submit' name='submit' value='Toggle Human' /></p>

<p><label id="titleform">Animal Detector</label><label>
<?php
if($animaldetector == 0){echo "No animal inside";}
elseif($animaldetector == 1){echo "Animal Inside";}
?></label><br />
<input class='entrysubmit' type='submit' name='submit' value='Toggle Animal' /></p>
</form>
</fieldset>
</td>
</tr>
</table>

<fieldset>
<legend id="fieldtitle" align="center">Light Status Form</legend>

<form id='lightForm' name='lightForm' method='post' enctype='multipart/form-data' action='cageProcess.php'>
<p><label id="entrytitle">Lighting</label><label>
<?php
if($light == 0){echo "Off";}
elseif($light == 1){echo "On";}
?>
</label>
<br />
<?php
echo "<input type='hidden' name='cageid' value='".$id . "' />";
echo "<input type='hidden' name='cagenumber' value='".$cagenumber . "' />";
echo "<input type='hidden' name='lighting' value='".$light . "' />";
?>
<input class="entrysubmit" type="submit" name="submit" id="submit" value="Toggle lighting" />
</p></form>
</fieldset>

<table id="zootable" width="100%" border="1">
  <tr>
    <td id="tableheader">Name</td>
    <td id="tableheader">Enclosure Number</td>
    <td id="tableheader">Size</td>
    <td id="tableheader">Type</td>
    <td id="tableheader">Exhibit Name</td>
    <td id="tableheader">Exhibit Description</td>
    <td id="tableheader">Lights</td>
  </tr>
  
	<?php
		echo "<tr>";
		echo "<td>". $name. "</td>";
		echo "<td>". $cagenumber . "</td>";
		echo "<td>". $size. "m</td>";
		echo "<td>". $type. "</td>";
		echo "<td>". $exhibitname. "</td>";
		echo "<td>". $exhibitdesc. "</td>";
		
		echo "<td>";
		if($light == 0){echo "Off";}
		elseif($light == 1){echo "On";}
		echo "</td>";
						
		echo "</tr>";
	?> 
</table>
<br />
<p>
<form id="cageForm" name="cageForm" method="post" enctype="multipart/form-data" action="cageProcess.php">
<input type="submit" name="submit" id="submit" value="Return to Zoo page" />
</form>
</p>
</body>
</html>
